<?php

class Email
{
	private $error;
	public $cookie_jar1;
	public function __construct()
	{}
	
	public function __destruct()
	{
		$this->clean();
	}

	public function get_contacts($account, $password)
	{
		$account = current(explode('@', $account));
		$account = strtolower(trim($account));
		$password = trim($password);
		if (empty($account) || empty($password)) {
			$this->error = 'err_01';
			return false;
		}
		$account .= '@yahoo.cn';

		$tmp_dir = PATH_SEPARATOR == ':' ? '/dev/shm/invite/' : 'c:/windows/temp/';
		$tmp_dir = $tmp_dir . substr(md5(microtime(true) . rand(0, 999999999)), 0, 2);

		$cookie_jar1 = tempnam($tmp_dir, 'cookie1');
		$this->cookie_jar1 = $cookie_jar1;

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_TIMEOUT, 3);
		curl_setopt($ch, CURLOPT_URL, 'https://edit.bjs.yahoo.com/config/login');
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, '.tries=1&.src=ym&.md5=&.hash=&.js=&.last=&promo=&.intl=cn&.bypass=&.partner=&.u=565jg854bus19&.v=0&.challenge=SnEoUZiM7Dz9KGhZoeBqd558iW9I&.yplus=&.emailCode=&pkg=&stepid=&.ev=&hasMsgr=1&.chkP=Y&.done=http%3A%2F%2Fmail.cn.yahoo.com%2Fycn_setcookie_inset.html%3F.done%3Dhttp%253A%252F%252Fmail.cn.yahoo.com%252Finset.html%253Frr%253D1144267962%26.remember%3Dn&.pd=ym_ver%3D0%26c%3D%26ivt%3D%26sg%3D&login=' . $account . '&passwd=' . $password);
		curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HEADER, true);
		curl_setopt($ch, CURLOPT_NOBODY, false);
		$return = curl_exec($ch);
		preg_match('/Location: (.*?)\s/', $return, $m);
		if (!isset($m[1])) {
			$this->error = 'err_01';
			curl_close($ch);
			return false;
		}

		curl_setopt($ch, CURLOPT_URL, $m[1]);
		curl_setopt($ch, CURLOPT_POST, false);
		$return = curl_exec($ch);

		preg_match('/<meta http-equiv="Refresh" content="0; url=(.*?)">/', $return, $m);
		if (!isset($m[1])) {
			$this->error = 'err_01';
			curl_close($ch);
			return false;
		}

		curl_setopt($ch, CURLOPT_URL, $m[1]);
		$return = curl_exec($ch);
		preg_match('/Location: (.*?)\s/', $return, $m);
		if (!isset($m[1])) {
			$this->error = 'err_01';
			curl_close($ch);
			return false;
		}

		$old_query = parse_url($m[1], PHP_URL_QUERY);
		curl_setopt($ch, CURLOPT_URL, 'http://cn.address.mail.yahoo.com/index.php?VPC=contact_list&' . $old_query);
		$return = curl_exec($ch);
		curl_close($ch);
		preg_match_all('/<span class="nickname">\((.*?)\)<\/span>/', $return, $m1);
		preg_match_all('/<a\s+href="http:\/\/mrd.mail.yahoo.com\/compose\?To=.*?">(.*?)<\/a>/', $return, $m2);
		if (!count($m1[1]) || count($m1[1]) != count($m2[1])) {
			$this->error = 'err_02';
			return false;
		}

		$contacts = array();
		foreach ($m1[1] as $k => $v) {
			$tmp = array();
			$tmp['email'] = iconv('UTF-8', 'GBK', $m2[1][$k]);
			$tmp['name'] = iconv('UTF-8', 'GBK', $v);
			array_push($contacts, $tmp);
		}
		if (!count($contacts)) {
			$this->error = 'err_02';
			return false;
		}
		return $contacts;
	}

	public function get_error()
	{
		return $this->error;
	}
	
	public function clean()
	{
		if(file_exists($this->cookie_jar1))
		{
			@unlink($this->cookie_jar1);
		}
	}
}

?>